package com.xx.job.paymenthystrix8083.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.netflix.hystrix.contrib.javanica.conf.HystrixPropertiesManager;
import com.xx.job.common.CommonResult;
import com.xx.job.paymenthystrix8083.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
// 全局降级注解
/*@DefaultProperties(defaultFallback = "hystrixTimeoutHandleAll",commandProperties = {
        @HystrixProperty(name = HystrixPropertiesManager.EXECUTION_ISOLATION_THREAD_TIMEOUT_IN_MILLISECONDS, value = "7000")
})*/
public class PaymentController {

    @Autowired
    private PaymentService paymentService;

    @RequestMapping("/payment/hystrix/paymentCircuitBreaker/{id}")
    public CommonResult paymentCircuitBreaker(@PathVariable Integer id){
        return new CommonResult(200, "",paymentService.paymentCircuitBreaker(id));
    }

    @RequestMapping("/payment/hystrix/ok/{id}")
    public CommonResult hystrixOk(@PathVariable String id){
        return new CommonResult(200, "",Thread.currentThread().getName()+"-------------"+id);
    }

    // 方法级别降级注解
    /*@HystrixCommand(fallbackMethod = "hystrixTimeoutHandle",commandProperties = {
            @HystrixProperty(name = HystrixPropertiesManager.EXECUTION_ISOLATION_THREAD_TIMEOUT_IN_MILLISECONDS, value = "3000")
    })*/
    @HystrixCommand
    @RequestMapping("/payment/hystrix/timeout/{id}")
    public CommonResult hystrixTimeout(@PathVariable String id) throws InterruptedException {
        System.out.println("ddddddddddddddddddd"+Thread.currentThread().getName());
        Thread.sleep(6000);
       // int a = 10/0;
        return new CommonResult(200, "",Thread.currentThread().getName()+"-------------"+id);
    }

    /**
     * 降级回调方法
     * @return
     */
    public CommonResult hystrixTimeoutHandle(@PathVariable String id) throws InterruptedException {
        return new CommonResult(400, "",Thread.currentThread().getName()+"-------------"+id+"错误");
    }

    /**
     * 全局降级回调方法
     * @return
     */
    public CommonResult hystrixTimeoutHandleAll() {
        return new CommonResult(400, "",Thread.currentThread().getName()+"-------------"+"全局错误");
    }
}
